package com.pzr.soccer.crawler.service.impl;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.pzr.soccer.match.domain.Game;
import com.pzr.soccer.match.model.GameWrapper;
import lombok.Data;
import org.jsoup.nodes.Element;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Data
class ExtractorHistory {

    ExtractorHistory(Element tr, String baseUrl,String league,String yearStr) {
        this.league=league;
        currentTime = tr.selectFirst("td.text-center.match_status").text();
        home = tr.selectFirst("td.text-right > a").text();
        score = tr.selectFirst("td.text-center.match_goal").text();
        corner = tr.selectFirst("td:nth-child(7)").text();
        halfCorner = tr.selectFirst("td:nth-child(8)").text();
        cornerOdds = tr.selectFirst("td:nth-child(9)").text();
        cornerDaxiao = tr.selectFirst("td:nth-child(10)").text();
        daXiaoPan = tr.selectFirst("td:nth-child(11)").text();
        daXiao = tr.selectFirst("td:nth-child(12)").text();
        yaPan = tr.selectFirst("td:nth-child(6)").text();

        danger = tr.selectFirst("td.text-center.match_attacks").text();
        shot = tr.selectFirst("td.text-center.match_shoot").text();
        stateUrl = baseUrl + tr.selectFirst("td:nth-child(15)> a:nth-child(3)").attr("href");

        kick_off = tr.selectFirst("td:nth-child(3)").text();
        away = tr.selectFirst("td.text-left > a").text();


        String begin=tr.selectFirst("td:nth-child(1)").text().trim(); //like: 12/01 04:15
        String[] arr=begin.split("\\s+");
        String month=arr[0].split("/")[0];
        String day=arr[0].split("/")[1];
        String localTimeStr=arr[1];

        actualKickOff=LocalDateTime.of(LocalDate.parse(yearStr+"-"+month+"-"+day),LocalTime.parse(localTimeStr));
        gameId=actualKickOff.toLocalDate()+"_"+home;
    }

    /*
      直接extract
     */
        String currentTime;
        String home;
        String score;
        String corner;
        String danger;
        String shot;
        String stateUrl;
        String league;
        String kick_off;
        String away;
        String yaPan;
        String daXiaoPan;
        String daXiao;
        String halfCorner;
        String cornerOdds;
        String cornerDaxiao;
        String gameId;

    /*
      计算出的
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    LocalDateTime actualKickOff;

    GameWrapper buildWrapper(){
        GameWrapper wrapper=new GameWrapper();
        Game game=new Game();
        wrapper.setGame(game);

        game.setId(gameId);
        game.setHome(home);
        game.setAway(away);
        game.setLeague(league);
        if (yaPan.contains("+")) {
            game.setStrongTeam("away");
        } else {
            game.setStrongTeam("home");
        }
        game.setScore(score);
        game.setYanPan(yaPan);
        game.setCorner(corner);
        game.setHalfCorner(halfCorner);
        game.setJiaoQiuPan(cornerOdds);
        game.setJiaoQiuDaXiao(cornerDaxiao);
        game.setDaXiao(daXiao);
        game.setDaXiaoPan(daXiaoPan);
        game.setDanger(danger);
        game.setShot(shot);
        game.setKickOff(actualKickOff);

        return wrapper;
    }

}
